package com.ruoyi.flowable.listener.scrap;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.equ.domain.EquScrap;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 报废-4-2监听：普通设备处置
 */
@Slf4j
@Component("scrapListener_4_2")
public class ScrapListener_4_2 implements ExecutionListener {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void notify(DelegateExecution execution) {
        try {
            HashMap<String, Object> commonMap = new HashMap<>();
            String dealMethod = execution.getVariable("dealMethod", String.class);
            String methodDetail = execution.getVariable("methodDetail", String.class);
            String companyName = execution.getVariable("companyName", String.class);
            String dealTime = execution.getVariable("dealTime", String.class);
            commonMap.put("处置方式", dealMethod);
            commonMap.put("具体处置方式", methodDetail);
            commonMap.put("处置单位名称", companyName);
            commonMap.put("处置日期", dealTime);
            //赋值
            EquScrap equScrap = execution.getVariable("equScrap", EquScrap.class);

            //设备类型
            String flag = execution.getVariable("flag", String.class);
            HashMap<String, Object> disposalInfoMap = new HashMap<>();
            if (!flag.equals("普通设备")) {
                disposalInfoMap = objectMapper.readValue(equScrap.getDisposalInfo(), new TypeReference<HashMap<String, Object>>() {});
            }
            disposalInfoMap.put("普通设备处置", commonMap);
            String disposalInfo = objectMapper.writeValueAsString(disposalInfoMap);

            equScrap.setDisposalInfo(disposalInfo);

            execution.setVariable("equScrap", equScrap);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
